<template>
  <ContentWrap v-if="pageStatus === 1">
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="paramsData" class="-mb-15px" label-width="auto">
      <el-form-item label="日志编号" prop="constructionSn">
        <el-input v-model="paramsData.constructionSn" class="!w-360px" clearable placeholder="请输入日志编号" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="施工项目" prop="projectTitle">
        <el-input v-model="paramsData.projectTitle" class="!w-360px" clearable placeholder="请输入施工项目" @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="施工时间" prop="constructionTime">
        <el-date-picker
          v-model="paramsData.constructionTime"
          class="!w-360px"
          type="datetimerange"
          placeholder="请选择施工时间"
          range-separator="~" 
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          @change="e => paramsData.constructionTime = [formatDateToYMDHMS(e[0]), formatDateToYMDHMS(e[1])]"
        />
      </el-form-item>

      <el-form-item>
        <el-button v-hasPermi="['wise:construction-logs:query']" @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['wise:construction-logs:export']" :loading="exportLoading" plain type="success"
          @click="handleExport"><Icon class="mr-5px" icon="ep:download" />导出</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap v-if="pageStatus === 1">
    <el-table v-loading="loading" :data="tableData" @selection-change="tableSelectionChange">
      <el-table-column type="selection" width="30" />
      <template v-for="item in tableColumn" :key="item.prop">
        <el-table-column
          :width="item.width"
          :align="item.align ? item.align : 'center'" 
          :label="item.label"
          :prop="item.prop"
        >
          <template #default="scope">
            <div v-if="item.prop == 'constructionTime'">
              <p>{{ scope.row.constructionTime ? formatDate(scope.row.constructionTime) : '-' }}</p>
            </div>
            <div v-else-if="item.prop == 'createTime'">
              <p>{{ scope.row.createTime ? formatDate(scope.row.createTime) : '-' }}</p>
            </div>
            <div v-else-if="item.prop == 'employeeName'">
                <div v-if="scope.row.employeeName">
                  {{ scope.row.employeeName }} <span v-if="scope.row.employeePhone">（{{ scope.row.employeePhone }}）</span>
                </div>
                <div v-else>-</div>
            </div>
            <div v-else>
              {{ scope.row[item.prop] || '-' }}
            </div>
          </template>
        </el-table-column>
      </template>

      <el-table-column :width="200" align="center" label="操作">
        <template #default="scope">
          <el-button v-hasPermi="['wise:construction-logs:query']" link type="primary" @click="openForm(scope.row.id)">
            查看
          </el-button>
        </template>
      </el-table-column>

    </el-table>


    <!-- 分页 -->
    <Pagination v-model:limit="paramsData.pageSize" v-model:page="paramsData.pageNo" :total="total" @pagination="getList" />
  </ContentWrap>


  <LogForm v-else ref="logFormRef" />
</template>
<script setup>
  import { formatDateToYMDHMS } from '@/utils/index'
  import { getDailyLogList, exportDailyLog } from '@/api/viewsApi/dailyLog'
  import { formatDate } from '@/utils/formatTime'
  import LogForm from './logForm.vue'
  import download from '@/utils/download'
  import {ref,reactive,provide,nextTick} from "vue"
  const paramsData = reactive({
    pageNo: 1,
    pageSize: 10,
    constructionSn: '',
    projectTitle: '',
    constructionTime: '',
    ids: []
  })
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const tableData = ref([]) // 列表的数据
  const tableColumn = ref([
    { prop: 'constructionSn', label: '日志编号', align: 'center', width: 230},
    { prop: 'projectTitle', label: '施工项目', align: 'center'},
    { prop: 'projectLeader', label: '负责人', align: 'center'},
    { prop: 'projectDepartment', label: '所属部门', align: 'center'},
    { prop: 'constructionTime', label: '施工时间', align: 'center', width: 160},
    { prop: 'createTime', label: '上传时间', align: 'center', width: 160},
    { prop: 'employeeName', label: '上传人', align: 'center'},
  ]) // 表格的列
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  const selectIds = ref([])
  const pageStatus = ref(1) // 1列表  2详情
  const logFormRef = ref()
  /** 查询角色列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await getDailyLogList(paramsData)

      tableData.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

  /** 搜索按钮操作 */
  const handleQuery = () => {
    paramsData.pageNo = 1
    getList()
  }
  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }
  const handleExport = async () => {
    if(selectIds.value.length > 0){
      paramsData.ids = selectIds.value
    }
    
    try {
      // 导出的二次确认
      await message.exportConfirm()
      // 发起导出
      exportLoading.value = true
      const data = await exportDailyLog(paramsData)
      download.excel(data, '施工日志列表.xls')
    } catch {
    } finally {
      exportLoading.value = false
      paramsData.ids = []
    }
  }
  const tableSelectionChange = (selection) => {
    selectIds.value = selection.map(item => item.id)
  }
  const openForm = (id) => {
    pageStatus.value = 2
    nextTick(() => {
      logFormRef.value.init(id)
    })
  }
  const goBack = () => {
    pageStatus.value = 1
  }
  provide('goBack', goBack)
  /** 初始化 **/
  onMounted(async() => {
    await getList()
  })
</script>
<style lang="less" scoped>

</style>
